package com.goodomen.garbage.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    private static final DateFormat dateFormatday2 = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 默认返回 yyyy-MM-dd hh:mm:ss 格式的日期字符串
	 * @param date
	 * @return
	 */
	public static String format(Date date){
        if (date == null) {
            return null;
        }
        return dateFormat.format(date);
    }

    public static String formatToDay(Date date){
        if (date == null) {
            return null;
        }
        return dateFormatday2.format(date);
    }

    public static Date format2(String date) {
        try {
            if (StringUtils.isNotBlank(date)) {
                return dateFormatday2.parse(date);
            }
        } catch (ParseException e) {

        }
        return null;
    }

	/**
	 * 返回指定日期格式的日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern){
		if (date == null || StringUtils.isBlank(pattern)) {
			return null;
		}
		return new SimpleDateFormat(pattern).format(date);
	}
	
	public static Date befrtDate(int day){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -day);
		return cal.getTime();
		
	}

	/**
	 * 获取指定时间在一年当中是第几周
	 * @return
	 */
	public static Integer getWeekOfYear(String time) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 获取指定时间在一年当中是第几月
	 * @return
	 */
	public static Integer getMonthOfYear(String time) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 获取指定时间的年份
	 * @return
	 */
	public static Integer getYear(String time) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}
	
	/**
     * 获取第几周的具体时间，如：第42周（2016/10/17--2016/10/23）
     *
     * @param dateFormat
     * @param week
     * @return
     */
    public static String getWeekDate(SimpleDateFormat dateFormat, Integer week) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(week.toString().substring(0, 4))); // 第几年
        cal.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(week.toString().substring(4, 6))); // 设置第几周
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        Date date = cal.getTime();
        String s = dateFormat.format(date);
        cal.add(Calendar.DATE, 6);
        date = cal.getTime();
        String e = dateFormat.format(date);
        return "第" + week.toString().substring(4, 6) + "周（" + s + "-" + e + "）";
    }
    /**
     * 获取某一年的最大周数
     * @param year
     * @return
     */
    public static Integer getMixWeekOfYear(Integer year){
    	//一年最少有52周,最多有53周
    	Integer week = 52;
    	SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY); //设置每周的第一天为星期一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//每周从周一开始
        cal.setMinimalDaysInFirstWeek(7);  //设置每周最少为7天       
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.WEEK_OF_YEAR, 53);
        //分别取得当前日期
        Date date = cal.getTime();
        String time = format.format(date);
        if(time.substring(0, 4).equals(year+"")){ //判断年度是否相符，如果相符说明有53个周。  
        	week = 53;
		}
        return week;
    }

    /**
     * @Description: 把指定时间转换成周
     * @return int 返回类型 （201701）周
     */
    public static int weekDate(String dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        try {
            time = sdf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取时间的年份
        Calendar datetime = Calendar.getInstance();
        datetime.setTime(time);
        String date = ""+datetime.get(Calendar.YEAR);
        //获取当前所属一年的周
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(time);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        if(week<10){
            date += "0"+week;
        }else {
            date += week;
        }
        return Integer.parseInt(date);
    }

    /**
     * @Description: 把指定时间转换成月
     * @date 2017/7/27
     * @return int 返回类型 （201701）月
     */
    public static int monthDate(String dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date time = null;
        try {
            time = sdf.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取时间的年份和月份
        Calendar datetime = Calendar.getInstance();
        datetime.setTime(time);
        String date = ""+datetime.get(Calendar.YEAR);
        int month = datetime.get(Calendar.MONTH)+1;
        if(month<10){
            date += "0"+month;
        }else {
            date += month;
        }
        return Integer.parseInt(date);
    }

    /**
     * 距离日期
     * @param day
     * @return
     */
    public static Date distanceDate(int day, Date date){
        if(day==0){
            return date;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -day);
        return cal.getTime();

    }

    /**
     * 获得指定日期的后一天
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = dateFormatday2.parse(specifiedDay);
        } catch (ParseException e) {

        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);

        String dayAfter = dateFormatday2.format(c.getTime());
        return dayAfter;
    }
}
